#include "../StdInc.h"

namespace GSTrader
{
	namespace TradeStrategy
	{
		CPortfolioManager::CPortfolioManager(CFramework* pFramework) : m_pFramework(pFramework), m_nNextId(0)
		{
		}


		CPortfolioManager::~CPortfolioManager()
		{
		}

		void CPortfolioManager::Add(CPortfolio* pPortfolio)
		{
			if (pPortfolio == NULL || pPortfolio->m_nID >= 0)
			{
				return;
			}

			pPortfolio->m_nID = m_nNextId++;
			m_mapPortfolio[pPortfolio->m_nID] = pPortfolio;
		}

		bool CPortfolioManager::Remove(int nId)
		{
			auto it = m_mapPortfolio.find(nId);
			if (it != m_mapPortfolio.end())
			{
				m_mapPortfolio.erase(it);
				return true;
			}

			return false;
		}

		void CPortfolioManager::OnExecutionReport(CExecutionReport* pReport)
		{
			if (pReport && pReport->m_nPortfolioId >= 0)
			{
				CPortfolio* pPortfolio = GetPortfolioById(pReport->m_nPortfolioId);
				if (pPortfolio)
				{
					pPortfolio->OnExecutionReport(pReport);
				}
			}
		}

		void CPortfolioManager::Clear()
		{
			for (auto it : m_mapPortfolio)
			{
			
			}

			m_mapPortfolio.clear();
			m_nNextId = 0;
		}

		CPortfolio* CPortfolioManager::GetPortfolioById(int nId)
		{
			auto it = m_mapPortfolio.find(nId);
			if (it != m_mapPortfolio.end())
				return it->second;

			return NULL;
		}
	}
}